package com.zc.trendsDispatch.task;

import com.ruoyi.common.core.redis.RedisCache;
import com.zc.common.constant.RedisKeyConstants;
import com.zc.core.message.CommonDeviceMessage;
import com.zc.core.message.DeviceMessage;
import com.zc.core.message.DeviceOfflineMessage;
import com.zc.core.message.DeviceOnlineMessage;
import com.zc.core.message.property.ReportPropertyMessage;
import com.zc.operator.domain.ChargeStationDeviceExtend;
import com.zc.operator.mapper.ChargeStationDeviceExtendMapper;
import com.zc.web.id.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * @Author glb
 * @Date 2024/3/30 11:53
 * @Version 1.0
 */

@Component("SerialPortDataAcquisitionTask")
public class SerialPortDataAcquisitionTask {

    @Resource    private RedisCache redisCache;
    @Resource
    private ChargeStationDeviceExtendMapper chargeStationDeviceExtendMapper;


    public void getData(){
        /*List<String> list=new ArrayList();
        list.add("01030061001A95DF");
        list.add("0103000A001465C7");
        list.forEach(message->{
            try {
                SerialPortManager.sendSerialPortData(message);
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });*/
       /* String a="\"Ua\":236.3,\"Ub\":0,\"Uc\":0,\"Ia\":0.05,\"Ib\":0,\"Ic\":0,\"Pa\":0.004,\"Pb\":0,\"Pc\":0,\"P\":0.004,\"Qa\":-0.002,\"Qb\":0,\"Qc\":0,\"Q\":-0.002,\"Fr\":50.02,\"Uab\":236.3,\"Ubc\":0,\"Uca\":236.3,\"EPI\":0.11,\"EPE\":0\n";
        Map<String, Double> map = new HashMap<>();
        String[] keyValuePairs = a.split(",");
        for (String pair : keyValuePairs) {
            String[] entry = pair.split(":");
            if (entry.length > 1) {
                map.put(entry[0], Double.valueOf(entry[1]));
            }
        }
        dataParsing(map,"01");*/
        Map<String, Double> map = new HashMap<>();
        map.put("Ua",1d);
        map.put("Ub",2d);
        map.put("Uc",3d);
        dataParsing(map,"01");
    }

    public void dataParsing(Map<String,Double> map,String address){
        Double p = map.get("P");
        ChargeStationDeviceExtend chargeStationDeviceExtend=new ChargeStationDeviceExtend();
        chargeStationDeviceExtend.setAddressTwo(address);
        List<DeviceMessage> messageList = new ArrayList<>();
        List<ChargeStationDeviceExtend> chargeStationDeviceExtends = chargeStationDeviceExtendMapper.selectChargeStationDeviceExtendList(chargeStationDeviceExtend);
        if(chargeStationDeviceExtends.size()>0){
            ChargeStationDeviceExtend chargeStationDeviceExtendData = chargeStationDeviceExtends.get(0);
            String stationCode = chargeStationDeviceExtendData.getStationCode();
            String deviceId = chargeStationDeviceExtendData.getDeviceId();
            redisCache.setCacheMapValue(RedisKeyConstants.STATION_DEVICE_REAL_POWER.replaceAll(":station:device", ":1001:"), deviceId, p);

            com.alibaba.fastjson.JSONObject collectors =new com.alibaba.fastjson.JSONObject();
            map.forEach((key, value) -> {
                System.out.println(key + ":" + value);
                collectors.put(key,value);
            });

            //设置设备属性
            com.alibaba.fastjson.JSONObject collectorData = new com.alibaba.fastjson.JSONObject();
            collectorData.put("timestamp", LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
            collectorData.put("properties", collectors);
            collectorData.put("messageId", IDGenerator.SNOW_FLAKE_STRING.generate());
            collectorData.put("deviceId", deviceId);
            collectorData.put("success",true);
            ReportPropertyMessage collectorMessage = ReportPropertyMessage.create();
            collectorMessage.fromJson(collectorData);
            messageList.add(collectorMessage);

            String comMunStatus="1";
            //设置设备状态
            CommonDeviceMessage messageCollector = "1".equals(comMunStatus) ? new DeviceOnlineMessage() : new DeviceOfflineMessage();
            messageCollector.setDeviceId(deviceId);
            messageList.add(messageCollector);
            //PlatformDeviceDataAccessSupport.handlerClientDeviceMessage(Flux.fromIterable(messageList));
        }
    }
    public static void main(String[] args) {
        //String a="\"Ua\":236.3,\"Ub\":0,\"Uc\":0";
        //String a="\"Ua\":236.3,\"Ub\":0,\"Uc\":0,\"Ia\":0.05,\"Ib\":0,\"Ic\":0,\"Pa\":0.004,\"Pb\":0,\"Pc\":0,\"P\":0.004,\"Qa\":-0.002,\"Qb\":0,\"Qc\":0,\"Q\":-0.002,\"Sa\":0.005,\"Sb\":0,\"Sc\":0,\"S\":0.005,\"Pfa\":0.876,\"Pfb\":0,\"Pfc\":0,\"Pf\":0.876,\"Fr\":50.02,\"Uab\":236.3,\"Ubc\":0,\"Uca\":236.3,\"EPI\":0.11,\"EPE\":0\n";
        String a="\"Ua\":236.3,\"Ub\":0,\"Uc\":0,\"Ia\":0.05,\"Ib\":0,\"Ic\":0,\"Pa\":0.004,\"Pb\":0,\"Pc\":0,\"P\":0.004,\"Qa\":-0.002,\"Qb\":0,\"Qc\":0,\"Q\":-0.002,\"Fr\":50.02,\"Uab\":236.3,\"Ubc\":0,\"Uca\":236.3,\"EPI\":0.11,\"EPE\":0\n";
        Map<String, String> map = new HashMap<>();
        String[] keyValuePairs = a.split(",");
        for (String pair : keyValuePairs) {
            String[] entry = pair.split(":");
            if (entry.length > 1) {
                map.put(entry[0], entry[1]);
            }
        }
        map.forEach((k,y)->{
            System.out.println(k+":"+y);
        });

    }


}
